from datetime import date

from django.http import HttpResponse
from django.shortcuts import render
from django.views import View

from booktest.models import BookInfo





class DBView(View):
    def get(self,request):
        '''提供读取的方法'''
        # 基本查询 get all count
        # book = BookInfo.objects.get(btitle='金瓶梅')
        # print(book)
        # print(book.btitle)
        #
        # books = BookInfo.objects.all()
        # my_list = [book.btitle for book in books]
        # print(my_list)
        #
        # func = BookInfo.objects.count()
        # print(func)

        # 过滤查询
        # get 过滤单一结果

        # filter 过滤出多个结果
        func = BookInfo.objects.filter(id=1)
        print(func)

        # contains  是否包含
        func1 = BookInfo.objects.filter(btitle__contains='门')
        print(func1)

        # startswith、endswith：以指定值开头或结尾
        func2 = BookInfo.objects.filter(btitle__iendswith='部')
        print(func2)

        # 空查询 isnull：是否为null
        func3 = BookInfo.objects.filter(btitle__isnull=False)
        print(func3)

        # 范围查询 in：是否包含在范围内
        func4 = BookInfo.objects.filter(id__in=[1,3,5])
        print(func4)



        # exclude 排除掉符合条件剩下的结果
        helens = BookInfo.objects.exclude(id=2)
        print(helens)


        return HttpResponse('DBView get')

    def post(self, request):
        '''提供写入的方法'''
        # save方法
        # book = BookInfo(
        #     btitle='金瓶梅',
        #     bpub_date=date(2020,8,8),
        #     bread=200,
        #     bcomment=100
        # )
        # book.save()

        # create方法
        # BookInfo.objects.create(
        #     btitle='当幸福来敲门',
        #     bpub_date=date(1998,10,18),
        #     bread=200,
        #     bcomment=100
        # )

        return HttpResponse('DBView post')

    def put(self,request):
        '''提供读取的方法'''
        return HttpResponse('DBView put')

    def delete(self,request):
        '''提供读取的方法'''
        return HttpResponse('DBView delete')

